package com.example.serverSide.service.impl;

import com.example.serverSide.service.ImgService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

@Service
public class ImgServiceImpl implements ImgService {
    @Override
    public String update(StandardMultipartHttpServletRequest request) {
        String name = "";
        HttpServletRequest request1 = request.getRequest();
        Map<String, MultipartFile> fileMap = request.getFileMap();
        for (String filename : fileMap.keySet()) {
            name = filename;
        }
        MultipartFile multipartFile = fileMap.get(name);
        String imgType = multipartFile.getOriginalFilename();
        String type = imgType.substring(imgType.lastIndexOf("."));
        byte[] bytes = new byte[0];
        try {
            bytes = multipartFile.getBytes();
            Files.write(Paths.get("E:\\trash\\"+name+type), bytes);
        } catch (IOException e) {
            return "img Update failed";
            //e.printStackTrace();
        }
        return "Update success";
    }
}
